package test_gui05;


import javax.swing.event.*;

import java.awt.event.*;

import javax.swing.*;

import java.awt.*; 
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;


public class projet extends JFrame //implements ActionListener
{
	
	private JLabel etat, message, lengthL, widthL, areaL;
	private JTextField lengthTF, widthTF, areaTF;
	private JButton calculateB, exitB;
	
	public projet() 
	{
	    setTitle("Appli Patoche - 2016");
	    setSize( 500, 500 );

	 // icon de l'appli
	    Image img = Toolkit.getDefaultToolkit().getImage("d:/server.png");
	    setIconImage(img);

		getContentPane().setLayout(new BorderLayout()); 
		StatusBar statusBar = new StatusBar(); 
		
		Container pane = getContentPane();
		
		//pane.setLayout(null); // disable layout manager - use absolute layout
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		/*
		addWindowListener(new WindowAdapter() { 
		
			public void windowClosing(WindowEvent event) 
			{
					System.exit(0); 
			}
		}); 

		*/
		
			
		etat = new JLabel("dfdsfdfds");
		message = new JLabel("rgregrh");
		
		//pane.add(etat);
		//pane.add(message);
		
		//this.setLocationRelativeTo(null);
	    //On dfinit le layout  utiliser sur le content pane
	    //Trois lignes sur deux colonnes
	   // this.setLayout(new GridLayout(5, 2, 10, 10));
	    //On ajoute le bouton au content pane de la JFrame
	    this.getContentPane().add(new JButton("1"));
	    //this.getContentPane().add(new JButton("2"));
	    //this.getContentPane().add(new JButton("3"));
	    //this.getContentPane().add(new JButton("4"));
	    this.getContentPane().add(new JButton("5"));

	    
	    
	    pane.setLayout(null);
	    
	    String text = "A JTextArea object represents a multiline area for displaying text. "
	            + "You can change the number of lines that can be displayed at a time, "
	            + "as well as the number of columns. You can wrap lines and words too. "
	            + "You can also put your JTextArea in a JScrollPane to make it scrollable.";
	    
	    JTextArea textArea2 = new JTextArea(text, 5, 10);
	    textArea2.setPreferredSize(new Dimension(100, 100)); 
	    JScrollPane scrollPane = new JScrollPane(textArea2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
	    textArea2.setLineWrap(true);

	    pane.add(scrollPane);
	    scrollPane.setBounds(50 , 100, 250, 50);	

	    
	    JButton b1 = new JButton("one");
	    JButton b2 = new JButton("two");
	    JButton b3 = new JButton("three");
	    JButton b4 = new JButton("4");
	    JButton b5 = new JButton("5");
	    JLabel  lbl = new JLabel("5");

	    lbl.setText("connard");
	    
	    
	    pane.add(b1);
	    pane.add(b2);
	    pane.add(b3);
	    pane.add(b4);
	    pane.add(b5);
	    pane.add(lbl);
	    
	    
	    Insets insets = pane.getInsets();
	    Dimension size = b1.getPreferredSize();
	    b1.setBounds(25 + insets.left, 5 + insets.top,
	                 size.width, size.height);
	    size = b2.getPreferredSize();
	    b2.setBounds(55 + insets.left, 40 + insets.top,
	                 size.width, size.height);
	    size = b3.getPreferredSize();
	    b3.setBounds(150 + insets.left, 15 + insets.top,
	                 size.width + 50, size.height + 20);


	    b4.setBounds(50 , 300, 50, 14); // x, y, largeur, hauteur
	    b5.setBounds(50 , 350, 50, 14);

	    lbl.setBounds(50 , 250, 50, 14);	
	  
	    
	    
	    this.setLayout(new BorderLayout());
	    
	    //getContentPane().add(BorderLayout.SOUTH, statusBar); 
		pane.add(BorderLayout.SOUTH, statusBar); 

		receive(socket1,"pipo"); 


	} 
	
	public static void main(String[] args) 
	{ 
		(new projet()).show(); 
	}
} 

class StatusBar extends JPanel implements ActionListener
{
	private JLabel statusLabel;

	public StatusBar()
	{
		SimpleDateFormat sdf = new SimpleDateFormat( "dd MMM yyyy hh:mm:ss" );
		String hhh =  sdf.format( new GregorianCalendar().getTime() ) ;
		
		setLayout(new BorderLayout(3, 3)); 
		statusLabel = new JLabel(hhh); 
		statusLabel.setBorder(BorderFactory.createLoweredBevelBorder()); 
		statusLabel.setForeground(Color.black);
		add(BorderLayout.CENTER, statusLabel); 
		
		JLabel dummyLabel1 = new JLabel(" dssqdgg "); 
		dummyLabel1.setBorder(BorderFactory.createLoweredBevelBorder()); 
		add(BorderLayout.EAST, dummyLabel1); 

//		JLabel Label2 = new JLabel(" hrqzq "); 
//		Label2.setBorder(BorderFactory.createLoweredBevelBorder()); 
//		add(BorderLayout.WEST, Label2); 

		 ImageIcon imageIcon = new ImageIcon("d:/server.png");
		 JLabel Label2 = new JLabel(imageIcon);
		 Label2.setBorder(BorderFactory.createLoweredBevelBorder()); 
		 add(BorderLayout.WEST, Label2); 

		
		   Timer t = new Timer(1000, this);
		    t.start( );
	} 
	
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		// TODO Auto-generated method stub
		  SimpleDateFormat sdf = new SimpleDateFormat( "dd MMM yyyy hh:mm:ss" );
		  String hhh =  sdf.format( new GregorianCalendar().getTime() ) ;
		  statusLabel.setText(hhh);
	}
	
	static void receive(Socket s, String msg)  throws Exception 
	{
	  	
		   byte[] mybytearray = new byte[1024];
		    InputStream is = s.getInputStream();
		    FileOutputStream fos = new FileOutputStream("d:/xs.txt");
		    BufferedOutputStream bos = new BufferedOutputStream(fos);
		    int bytesRead = is.read(mybytearray, 0, mybytearray.length);
		    bos.write(mybytearray, 0, bytesRead);
		    
		    System.out.println("Reception du fichier"+msg);
		    
		    bos.close();
		    s.close();

	  }
	
}

